#include "HelloWorldScene.h"
#include "qdebug.h"
#include "ccnodeext.h"
#include "CCQTSceneDirector.h"
#include "properties/pefactory.h"
#include "AppDelegate.h"
#include "constantManager.h"
USING_NS_CC;

CCScene* HelloWorld::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::node();
	
	// 'layer' is an autorelease object
	HelloWorld *layer = HelloWorld::node();

	// add layer as a child to scene
	scene->addChild(layer);
	

	// return the scene
	return scene;
}

void HelloWorld::test(CCObject *pSender)
{
    
    
    
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}

	setIsTouchEnabled(true);
   // CCMenuItem * item = CCMenuItemFont::itemFromString("Test" , this , menu_selector(HelloWorld::test));

    
    //CCMenuItemImage *itemimage = CCMenuItemImage::itemFromNormalImage("ball.png" , "ball.png" , "ball.png" , this, menu_selector(HelloWorld::test)); 
    
    //CCMenu * menu = CCMenu::menuWithItems(item ,itemimage ,  0);
    
    //this->addChild(menu);
    
//    CCLabelTTF * label = CCLabelTTF::labelWithString("test" , "courier new" , 15);
//    this->addChild(label);

//     CCParticleSun *m_emitter = CCParticleSun::node();
// 
//     m_emitter->setTexture( CCTextureCache::sharedTextureCache()->addImage("ball.png") );
//     
//     this->addChild(m_emitter);
//     

// 	CCSprite * sprite = CCSprite::spriteWithFile("ball.png");
// 	CCRect rect = CCRectZero;
// 	CCTexture2D * pTexture = CCTextureCache::sharedTextureCache()->addImage("bg.png");
// 	rect.size = pTexture->getContentSize();
// 	sprite->setTexture(pTexture);
// 	sprite->setTextureRect(rect);
// 	this->addChild(sprite);
// 
//     qDebug()<< "init";
//     CCNodeExt *nodext = CCNodeExt::node();
//     this->addChild(nodext);

	return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_QT)
	exit(0);
#endif
}

void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
	CCNode * node = CCQTSceneDirector::sharedSceneDirector()->CurrentCNode();
	if (node == NULL) return;
    CCTouch * touch = (CCTouch*)pTouches->anyObject();
    CCPoint p = touch->locationInView(0);
    p = CCDirector::sharedDirector()->convertToGL(p);

    
    CCRect rect = node->boundingBox();
    if (node->getParent()!=0)
    {
        rect.origin = node->getParent()->convertToWorldSpace(rect.origin);
    }
	 
	if (CCRect::CCRectContainsPoint(rect,p ) )
	{
		//s_pointx = ccp(p.x - rect.origin.x , p.y - rect.origin.y);
        node->setIsSelected(true);

	}else
	{
        node->setIsSelected(false);
	}

}

void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
	CCNode * node = CCQTSceneDirector::sharedSceneDirector()->CurrentCNode();
	if (node == NULL) return;
//    qDebug()<< "ccTouchesMoved";
//	if (node->getIsSelected())
//	{
		CCTouch * touch = (CCTouch*)pTouches->anyObject();
		CCPoint p = touch->locationInView(0);
		CCPoint plast = touch->previousLocationInView(0);
		p = CCDirector::sharedDirector()->convertToGL(p);
		plast = CCDirector::sharedDirector()->convertToGL(plast);
		CCPoint p_vector = ccpSub(p , plast);
		//node->setPosition(ccp(p.x - s_pointx.x , p.y - s_pointx.y));
		node->setPosition(ccpAdd(p_vector , node->getPosition()));
        //PEFactory::shareFactory()->getPEditorByName("CCNode")->refreshProperty("Position");
        QString s;
        s.append("x : ").append(QString::number(p.x)).append("  y : ").append(QString::number(p.y));
        ConstantManager::sharedManager()->qWindow()->statusBar()->showMessage(s);
//	}

}

void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
//    qDebug()<< "ccTouchesEnded";
	CCNode * node = CCQTSceneDirector::sharedSceneDirector()->CurrentCNode();
	if (node == NULL) return;
	//    qDebug()<< "ccTouchesMoved";
//	if (node->getIsSelected())
//	{
		PEFactory::shareFactory()->getPEditorByName("CCNode")->refreshProperty("Position");
        ConstantManager::sharedManager()->qWindow()->statusBar()->showMessage("ready!");
//	}

}
